package com.mybatis.service.impl;

import com.mybatis.dto.request.QueryGenerationParam;
import com.mybatis.entity.GeneratorCodeLogEntity;
import com.mybatis.mapper.GeneratorCodeLogMapper;
import com.mybatis.message.Result;
import com.mybatis.message.ReturnResult;
import com.mybatis.service.GeneratorCodeLogService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import static com.mybatis.entity.table.GeneratorCodeLogEntityTableDef.GENERATOR_CODE_LOG_ENTITY;
import static com.mybatisflex.core.query.QueryMethods.noCondition;

/**
 * 代码生成的日志 服务层实现。
 *
 * @author xhs
 * @since 2023-09-12
 */
@Slf4j
@Service
public class GeneratorCodeLogServiceImpl extends ServiceImpl<GeneratorCodeLogMapper, GeneratorCodeLogEntity> implements GeneratorCodeLogService {

    /**
     * 分页查询代码生成记录
     *
     * @param generationParam 查询条件
     * @return ReturnResult<Object>
     */
    @Override
    public ReturnResult<Object> queryGenerationLog(QueryGenerationParam generationParam) {
        Page<GeneratorCodeLogEntity> page = new Page<>(generationParam.getPageNumber(), generationParam.getPageSize());
        Page<GeneratorCodeLogEntity> entityPage = queryChain().select(GENERATOR_CODE_LOG_ENTITY.DEFAULT_COLUMNS)
                .from(GENERATOR_CODE_LOG_ENTITY)
                .where(StringUtils.hasLength(generationParam.getDataBaseType()) ? GENERATOR_CODE_LOG_ENTITY.DATA_BASE_TYPE.like(generationParam.getDataBaseType()) : noCondition())
                .and(StringUtils.hasLength(generationParam.getDataBaseUrl()) ? GENERATOR_CODE_LOG_ENTITY.DATA_BASE_URL.like(generationParam.getDataBaseUrl()) : noCondition())
                .and(StringUtils.hasLength(generationParam.getDataBaseName()) ? GENERATOR_CODE_LOG_ENTITY.DATA_BASE_NAME.like(generationParam.getDataBaseName()) : noCondition())
                .and(StringUtils.hasLength(generationParam.getAuthor()) ? GENERATOR_CODE_LOG_ENTITY.AUTHOR.like(generationParam.getAuthor()) : noCondition())
                .orderBy(GENERATOR_CODE_LOG_ENTITY.CREATE_TIME.desc())
                .page(page);
        return ReturnResult.build(Result.QUERY_SUCCESS).setData(entityPage);
    }
}
